//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Configuration;
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Resource.Tests
{
	[TestClass]
	public class ResourceManagerFactoryFixture
	{
		private static ResourceManagerFactory factory;

		[TestInitialize]
		public void CreateFactory ()
		{
			factory = new ResourceManagerFactory(TestConfigurationSource.GenerateConfiguration());
		}

		[TestMethod]
		public void CreateNamedResourceInstance ()
		{
			ResourceManager resource = factory.Create("AssemblyResourceManager");
			Assert.IsNotNull(resource, "Should have created resource instance through factory");
		}

		[TestMethod]
		[ExpectedException(typeof(ConfigurationErrorsException))]
		public void WillThrowExceptionIfCannotFindResourceInstance ()
		{
			factory.Create("ThisIsABadName");

			Assert.Fail("Should have thrown ConfigurationErrorsException");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void WillThrowExceptionIfInstanceNameIsNull ()
		{
			factory.Create(null);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void WillThrowExceptionIfInstanceNameIsEmptyString ()
		{
			factory.Create("");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void WillThrowExceptionIfNullResourceProvider ()
		{
			factory.Create("ResourceManagerWithBadResourceProviderInstance");
		}

		[TestMethod]
		[ExpectedException(typeof(ConfigurationErrorsException))]
		public void WillThrowExceptionIfCannotCreateNamedProviderType ()
		{
			factory.Create("ResourceManagerWithBadResourceProviderType");
		}

		[TestMethod]
		public void CallingSameFactoryTwiceReturnsSameInstance ()
		{
			ResourceManager resourceOne = factory.Create("AssemblyResourceManager");
			ResourceManager resourceTwo = factory.Create("AssemblyResourceManager");
			Assert.AreSame(resourceOne, resourceTwo, "ResourceManagerFactory should always return the same instance when using the same instance name");
		}

		[TestMethod]
		public void CallingDifferentFactoryTwiceReturnsDifferentInstances ()
		{
			ResourceManager resourceOne = factory.Create("AssemblyResourceManager");

			ResourceManagerFactory secondFactory = new ResourceManagerFactory(TestConfigurationSource.GenerateConfiguration());
			ResourceManager resourceTwo = secondFactory.Create("AssemblyResourceManager");

			Assert.IsFalse(object.ReferenceEquals(resourceOne, resourceTwo), "Different factories should always return different instances for same instance name");
		}

		[TestMethod]
		public void CanCreateDefaultResourceManager ()
		{
			ResourceManager resourceManager = factory.CreateDefault();
			Assert.IsNotNull(resourceManager);
		}

		[TestMethod]
		public void DefaultResourceManagerAndNamedDefaultInstanceAreSameObject ()
		{
			ResourceManager defaultInstance = factory.CreateDefault();
			ResourceManager namedInstance = factory.Create("AssemblyResourceManager");

			Assert.AreSame(defaultInstance, namedInstance);
		}
	}
}